package com.yc.utils;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

/**
 * 
 * 南华   木卓子
 * @author May 27, 2021
 * Email: 1638982163@qq.com
 */
public class SWTUtil {
	/**
	 * 设置窗口居中
	 * @param shell
	 */
	public static void centerWindow(Shell shell) {
		Dimension dm = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小
		shell.setLocation((dm.width - shell.getSize().x) / 2, (dm.height - shell.getSize().y) / 2);
	}
	


	/**
	 * 设置图片自适应   填充
	 * @param composite
	 * @param x
	 * @param y
	 */
	public static void imagetSize(Composite composite) {
		Image image = composite.getBackgroundImage(); // 获取面板图片
		composite.addPaintListener(new PaintListener() {
			// 监听面板
			@Override
			public void paintControl(PaintEvent e) {
				int h = composite.getBounds().height; // 获取面板的高度和宽度
				int w = composite.getBounds().width;
				
				int height = image.getBounds().height; // 获取图片的高度和宽度
				int width = image.getBounds().width;
				e.gc.drawImage(image,  0, 0, width, height, 0, 0, w, h);
			}
		});
	}
	
	/**
	 * 上传图片的自适应
	 * @param composite 承载图片的面板
	 * @param in 图片的流
	 * @return
	 */
	public static Image imagetSize(Composite composite, InputStream in) {
		ImageData imageData = new ImageData(in);
		imageData = imageData.scaledTo(composite.getBounds().width, composite.getBounds().height);
		return new Image(null, imageData);
	}
	
	public static void showMessage(Shell shell, String name) {
		MessageBox messageBox = new MessageBox(shell, SWT.NONE);
		messageBox.setMessage(name);
		messageBox.open();
	}
	
	public static Image imageSize(int width, int height, InputStream in) {
		ImageData imageData = new ImageData(in);
		if(imageData.width <= width) {
			width = imageData.width;
		}
		if(imageData.height <= height) {
			height = imageData.height;
		}
		imageData = imageData.scaledTo(width, height);
		return new Image(null, imageData);
	}
	
	public static void imageSize(Label label, int h0) {
		Image image = label.getImage();
		//监听面板
		label.addPaintListener(new PaintListener() {
			@Override
			public void paintControl(PaintEvent e) {
				int h = label.getBounds().height;
				int w = label.getBounds().width;
				
				int height = image.getBounds().height;
				int width = image.getBounds().width;
				//图片自适应
				e.gc.drawImage(image, 0, 155 * h0, width, 117, 0, 0, w, h);
			}
		});
	}


	
	public static void imageSize(Label label) {
		Image image = label.getImage();
		//监听面板
		label.addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent e) {
				int h = label.getBounds().height;
				int w = label.getBounds().width;
				
				int height = image.getBounds().height;
				int width = image.getBounds().width;
				//图片自适应
				e.gc.drawImage(image, 0, 0, width, height, 0, 0, w, h);
			}
		});
	}
	
	/**
	 * 上传图片的自适应
	 * @param composite
	 * @param in
	 * @return
	 */
	public static Image imageSize(Composite composite, InputStream in) {
		ImageData imageData = new ImageData(in);
		imageData = imageData.scaledTo(composite.getBounds().width, composite.getBounds().height);
		return new Image(null, imageData);
	}
	
	public static void imagetSize(Button button) {
		Image image = button.getImage(); // 获取面板图片
		button.addPaintListener(new PaintListener() {
			// 监听面板
			@Override
			public void paintControl(PaintEvent e) {
				int h = button.getBounds().height; // 获取面板的高度和宽度
				int w = button.getBounds().width;
				
				int height = image.getBounds().height; // 获取图片的高度和宽度
				int width = image.getBounds().width;
				e.gc.drawImage(image,  0, 0, width, height, 0, 0, w, h);
			}
		});
	}
	
	public static void imagetSize(Label label) {
		Image image = label.getImage(); // 获取面板图片
		label.addPaintListener(new PaintListener() {
			// 监听面板
			@Override
			public void paintControl(PaintEvent e) {
				int h = label.getBounds().height; // 获取面板的高度和宽度
				int w = label.getBounds().width;
				
				int height = image.getBounds().height; // 获取图片的高度和宽度
				int width = image.getBounds().width;
				e.gc.drawImage(image,  0, 0, width, height, 0, 0, w, h);
			}
		});
	}

	
}
